package com.boyan.order.service.impl;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.boyan.order.bean.Order;
import com.boyan.order.feign.ProductFeignClient;
import com.boyan.order.service.OrderService;
import com.boyan.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    ProductFeignClient productFeignClient;


    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId,Long userId) {
        //Product product = gerProductFromRemoteWithLoadBalanceAnnotation(productId);

        //使用feign调用远程服务
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();

//        try {
//            SphU.entry("createOrder");
//        } catch (BlockException e) {
//            throw new RuntimeException(e);
//        }

        order.setId(1L);
        order.setUserId(userId);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setNickName("zhangsan");
        order.setAddress("BY");
        order.setProductList(Arrays.asList(product));


        return order;

    }

    //兜底回调
    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
        log.info("createOrderFallback...............");
        Order order = new Order();
        order.setId(0L);
        order.setUserId(userId);
        order.setTotalAmount(new BigDecimal(0));
        order.setNickName("位置用户");
        order.setAddress("异常信息"+e.getClass());
        order.setProductList(null);
        return order;
    }


    //方法3注解
    private Product gerProductFromRemoteWithLoadBalanceAnnotation(Long productId) {

        String url="http://service-product/product/"+productId;
        log.info("远程请求路径:{}",url);
        return restTemplate.getForObject(url,Product.class);
    }

    private Product gerProductFromRemote(Long productId) {

        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

        ServiceInstance instance = instances.get(0);
        String url="http://"+instance.getHost()+":"+instance.getPort()+"/product/"+productId;

        log.info("远程请求路径:{}",url);
        return restTemplate.getForObject(url,Product.class);
    }



    private Product gerProductFromRemote2(Long productId) {

        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url="http://"+choose.getHost()+":"+choose.getPort()+"/product/"+productId;
        log.info("远程请求路径:{}",url);

        return restTemplate.getForObject(url,Product.class);

    }
}
